/**
  ******************************************************************************
  *Copyright(C),Tongji University
  * @file           : ESP12F_stm32f4xx_hal.c
  * @brief          : contain the ESP12F functions 
  * Author			: ultralyj
  * Date			: 2020.10.27
  ******************************************************************************
  */

#include "ESP12F_stm32f4xx_hal.h"
#include "main.h"

extern UART_HandleTypeDef huart2;
uint8_t uart2value=0;
void HAL_ESP12_SendData(char* p);

/**
  * @brief ESP12F Initialization Function
  * @retval None
  */
void HAL_ESP12_Init(void)
{
	/* awake the device */
	HAL_ESP12_SendData("AT\r\n");
	HAL_Delay(100);
	/* ESP12F will set a wifi port */
	HAL_ESP12_SendData("AT+CWMODE=2\r\n");
	HAL_Delay(100);
	/* configration the name, password of the wifi */
	HAL_ESP12_SendData("AT+CWSAP=\"TCS_Port\",\"510emperor\",11,3\r\n");
	HAL_Delay(100);
	HAL_ESP12_SendData("AT+CIPMUX=1\r\n");
	/* establish the TCP server */
	HAL_Delay(100);
	HAL_ESP12_SendData("AT+CIPSERVER=1,8080\r\n");
	HAL_Delay(100);
	/* print the IP address, (just for test) always 192.168.4.1 */
	HAL_ESP12_SendData("AT+CIFSR\r\n");
	HAL_Delay(100);	
	/* start receiving data */
	HAL_UART_Receive_IT(&huart2, (uint8_t *)&uart2value,1); 
}

/**
  * @brief Send data to ESP12 through uart2
  * @param p, pointer
  * @retval None
  */
void HAL_ESP12_SendData(char *p)
{
	uint16_t len = 0;
	char *p1 = p;
	printf("[ESP]%s",p);
	while (*p1++ != 0)
		len++;
	HAL_UART_Transmit(&huart2, (uint8_t *)p, len, 0xfff);
}
